package com.bjesc_b2c.app.appraiset.widget.popupwindow;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.bjesc_b2c.app.appraiset.R;
import com.bjesc_b2c.app.appraiset.adapter.SelectCarBrandAdapter;
import com.bjesc_b2c.app.appraiset.widget.BasePopupWindow;
import com.bjesc_b2c.app.appraiset.widget.view.IndexView;

import java.util.List;

import app.pinggushi.bjesc.com.baserecyclerview.adapter.MultiItemTypeAdapter;
import master_bean.SelectCarBrandBean;

/**
 * Created by yidong on 2017/5/8.
 */

public class CarFilterPop extends BasePopupWindow implements MultiItemTypeAdapter.OnItemClickListener, IndexView.OnSetIndexListenre{
    private RecyclerView recyclerView;
    private IndexView indexView;
    private SelectCarBrandAdapter brandAdapter;
    private List<SelectCarBrandBean> brandList;
    private LinearLayoutManager manager;
    private Context context;
    private CarBrandSelectListener selectListener;

    public CarFilterPop(Context context,List<SelectCarBrandBean> brandList,CarBrandSelectListener selectListener) {
        super(context,null);
        this.context = context;
        this.brandList = brandList;
        this .selectListener = selectListener;
        brandAdapter = new SelectCarBrandAdapter(context, R.layout.layout_pop_item_car_brand, brandList);
        manager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(brandAdapter);
        recyclerView.setOnScrollListener(scrollListener);
        brandAdapter.setOnItemClickListener(this);
    }
    @Override
    protected void initViews() {
        view = inflater.inflate(R.layout.layout_pop_car_brand,null);
        recyclerView = (RecyclerView)view.findViewById(R.id.recycler_view);
        indexView = (IndexView)view.findViewById(R.id.index_view);
        indexView.setOnSetIndexListenre(this);
    }

    @Override
    protected void setView() {
        super.setView();
        this.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    @Override
    protected int setRootViewId() {
        return R.id.root;
    }

    private RecyclerView.OnScrollListener scrollListener = new RecyclerView.OnScrollListener() {

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            int position = manager.findFirstVisibleItemPosition();
            indexView.refresh(brandList.get(position).getFirstLetter().charAt(0));
        }
    };

    @Override
    public void setIndexListenre(char index) {
        for (int i = 0; i < brandList.size(); i++) {
            if (String.valueOf(index).equals(brandList.get(i).getFirstLetter())) {
                manager.scrollToPositionWithOffset(i, 0);
                break;//加了break显示才会更准确—yd
            }
        }
    }
    @Override
    public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
        for (int i = 0; i < brandList.size(); i++) {
            if (i == position) {
                brandList.get(i).setCheck(true);
            } else {
                brandList.get(i).setCheck(false);
            }
        }
        indexView.refresh(brandList.get(position).getFirstLetter().charAt(0));
        selectListener.getBrandId(brandList.get(position).getId(),brandList.get(position).getName());
        brandAdapter.notifyDataSetChanged();
        dismiss();
    }

    @Override
    public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
        return false;
    }
    public interface CarBrandSelectListener{
        void getBrandId(String brandId,String brandName);
    }

}

